import { defineStore } from 'pinia'
import { ref } from 'vue'

// 背诵单词模块
export const useWordsStore = defineStore('words', () => {
  const words = ref([])

  // 清空仓库的方法
  const removeWords = () => {
    words.value = []
  }

  const setWords = (newWords) => {
    words.value = newWords
    //给每一项 全部添加一个从1开始按照顺序排列的索引
    for (let i = 1; i <= words.value.length; i++) {
      words.value[i - 1].index = i
    }
  }

  // 通过过滤找出对象中当前idnex的返回
  const getById = (index) => {
    return words.value.find((item) => item.index === index)
  }

  // 删除当前index 项
  const deleteWord = (index) => {
    words.value = words.value.filter((item) => item.index !== index)
    return words.value
  }

  //把id 最小的返回
  const getSlammIdWord = () => {
    return words.value[0]
  }

  return {
    words,
    setWords,
    getSlammIdWord,
    getById,
    deleteWord,
    removeWords
  }
})
